package com.goldenautumn.prefabricate.activity.account

import android.os.Bundle
import android.view.animation.AnimationUtils
import androidx.activity.viewModels
import androidx.fragment.app.FragmentActivity
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.VibrateUtils
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.business.user.LoadingUiState
import com.goldenautumn.prefabricate.business.user.LoginUiState
import com.goldenautumn.prefabricate.business.user.UserViewModel
import com.goldenautumn.prefabricate.databinding.ActivityLoginBinding
import com.goldenautumn.prefabricate.utils.Constants
import com.goldenautumn.prefabricate.utils.Constants.BUSINESS_PAGE_DATA
import com.goldenautumn.prefabricate.utils.Constants.BUSINESS_PAGE_DATA_PARAMS
import com.goldenautumn.prefabricate.utils.Constants.SEARCH_HISTORY_INSERT
import com.goldenautumn.prefabricate.utils.onCenterToast
import com.goldenautumn.prefabricate.widget.dialog.CountrySelectDialog
import com.gyf.immersionbar.ImmersionBar
import kotlinx.coroutines.isActive
import kotlinx.coroutines.selects.select
import qncx.external.common.base.BaseActivity
import qncx.external.common.utils.TUIRouter
import qncx.external.common.utils.intent
import qncx.external.common.widget.colortext.ITextListener
import qncx.external.common.widget.view.TipsDialog

class LoginActivity : BaseActivity<ActivityLoginBinding>(), ITextListener {

    private val mUserModel by viewModels<UserViewModel>()

    override fun getLayoutId(): Int = R.layout.activity_login

    override suspend fun main() {
        initImmersionBar()
        ImmersionBar.setTitleBar(this, mBinding.toolbar)
        mBinding.backBtn.setOnClickListener { onBackPressedDispatcher.onBackPressed() }

        mBinding.loginBtn.setOnClickListener {
            var phone = mBinding.phone.text.toString()
            if(phone.length > 8 ) mUserModel.getCode(phone)
            else {
                mBinding.loginBtn.reset()
                VibrateUtils.vibrate(50)
                mBinding.phone.startAnimation(AnimationUtils.loadAnimation(it.context, R.anim.shake_anim))
            }
        }
        mBinding.country.setOnClickListener {
            CountrySelectDialog(1){

            }.onShow(supportFragmentManager.beginTransaction())
        }

//        var arr = resources.getStringArray(R.array.user_agreement)
//        var spannableText =  SpannableText(mBinding.ruleTv.context,this)
//        spannableText.setParam(getString(R.string.check_agreements),arr[0],arr[1], arr[0], arr[1])
//        spannableText.setTargetStyle(qncx.external.common.R.color.colorPrimary, false)
//        spannableText.setTextView(mBinding.ruleTv)

        while (isActive) {
            select {
                mUserModel.loading.onReceive {
                    when (it) {
                        is LoadingUiState.LoadingUI -> {
                            LogUtils.d("加载状态=${it.isShow}")
                        }
                    }
                }
                mUserModel.loginRequests.onReceive {
                    when (it) {
                        is LoginUiState.LoginSuccess -> {
                            LogUtils.d("${it.userInfo}")
                        }

                        is LoginUiState.LoginFail -> {
                            TipsDialog.Builder(this@LoginActivity)
                                .setIcon(qncx.external.common.R.drawable.tips_warning_ic)
                                .setMessage(it.msg)
                                .show()
                        }
                        is LoginUiState.CodeResult -> {
                            if(it.msg == null) startActivity(
                                VerificationActivity::class.intent.putExtra(
                                    BUSINESS_PAGE_DATA,
                                    SEARCH_HISTORY_INSERT
                                ).putExtra(BUSINESS_PAGE_DATA_PARAMS, mBinding.phone.text.toString())
                                    .putExtra("AREA","86")
                            ) else onCenterToast("${it.msg}")
                            mBinding.loginBtn.reset()
                        }
                    }
                }
            }
        }
    }

    override fun onClickText(url: String) {
        var bundle = Bundle()
        bundle.putString("START_ACTIVITY_PARAMS", url)
        TUIRouter.startActivityForResult(
            ActivityUtils.getTopActivity() as FragmentActivity,
            "ProtocolActivity",
            bundle,
            null
        )
    }


}